package com.example.springvue.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.springvue.common.annotation.LogAnnotation;
import com.example.springvue.common.annotation.PassToken;
import com.example.springvue.common.handler.AdminUserHeaderHodler;
import com.example.springvue.common.handler.NotExistException;
import com.example.springvue.common.returnthrow.Result;
import com.example.springvue.common.utils.CommonUtils;
import com.example.springvue.model.AdminUser;
import com.example.springvue.model.AuthGroup;
import com.example.springvue.service.AdminUserService;
import com.example.springvue.service.AuthGroupService;
import com.example.springvue.service.AuthRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    AuthGroupService authGroupService;
    @Autowired
    AuthRuleService authRuleService;


    @RequestMapping("/admin/user/userInfo")
    @ResponseBody
    @LogAnnotation(module = "用户模块",operator = "获取登录用户信息")
    public Result<Map<String ,Object>> queryUserList(){
        AdminUser adminUser = AdminUserHeaderHodler.get();
        Map<String ,Object> userInfo = new HashMap<>();
        userInfo.put("id",adminUser.getId());
        userInfo.put("nickname",adminUser.getNickname());
        userInfo.put("username",adminUser.getUsername());
        userInfo.put("email",adminUser.getEmail());
        userInfo.put("avatar",adminUser.getAvatar());
        userInfo.put("avatar2", CommonUtils.image(adminUser.getAvatar()));
        return Result.success("请求成功",userInfo);
    }



    /**
     * 管理员列表
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PostMapping(value = "/admin/user/list")
    public Result<Map< String, Object >> getAdminUserList(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int pageSize = (int) Body.get("pageSize");
            int page = (int) Body.get("page");
            Map<String, Object>  authRule = adminUserService.getAdminUserList(pageSize,page);
            return Result.success("请求成功",authRule);
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }



    /**
     *
     * 保存
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PostMapping(value = "/admin/user/save")
    public Result<Boolean> adminUserSave(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int id = (int) Body.get("id");
            if (id == 0){
                AdminUser adminUsers = adminUserService.getAdminUserInfo((String) Body.get("username"));
                System.out.println(adminUsers);
                if (CommonUtils.isNotEmptyObject(adminUsers)) {
                    return Result.failure("添加失败,账号已存在");
                }

                boolean add= adminUserService.getAdminUserAdd(Body);
                if (add){
                    return Result.success("添加成功",null);
                }else {
                    return Result.failure("添加失败");
                }
            }else {
                AdminUser adminUser = adminUserService.getAdminUserInfoId(id);
                if (adminUser.equals(null)){
                    return Result.failure("用户不存在");
                }
                Body.put("salt",adminUser.getSalt());
                boolean edit= adminUserService.AdminUserEdit(Body);
                if (edit){
                    return Result.success("修改成功",null);
                }else {
                    return Result.failure("修改失败");
                }
            }
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }

    /**
     *
     * 删除
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PostMapping(value = "/admin/user/delete")
    public Result<Boolean> AdminUserDelete(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            String id = (String) Body.get("id");
            String[] strArray = id.split(",");
            boolean del = adminUserService.AdminUserDelete(strArray);
            if (del){
                return Result.success("删除成功",null);
            }else {
                return Result.failure("删除失败");
            }

        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }
}
